package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDetailService orderDetailService;


    public void submit(Orders orders) {
        //1.数据准备,获取当前用户购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        if (CollectionUtils.isEmpty(list)) {
            throw new CustomException("购物车为空,不能下单");
        }

        //1.2 获取收货地址信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());

        //1.3获取当前登录用户信息
        User user = userService.getById(currentId);

        long orderid = IdWorker.getId();
        BigDecimal bigDecimal = new BigDecimal("0");

        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart shoppingCart : list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderid);
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setNumber(shoppingCart.getNumber());

            //计算总金额
            //计算总金额
            bigDecimal = bigDecimal.add(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())));
            orderDetails.add(orderDetail);

        }

        /*//2.组装订单明细
         list.stream().map(shoppingCart ->
                 {
                     OrderDetail orderDetail = new OrderDetail();
                     orderDetail.setOrderId(orderid);
                     orderDetail.setAmount(shoppingCart.getAmount());
                     orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
                     orderDetail.setDishId(shoppingCart.getDishId());
                     orderDetail.setSetmealId(shoppingCart.getSetmealId());
                     orderDetail.setImage(shoppingCart.getImage());
                     orderDetail.setName(shoppingCart.getName());
                     orderDetail.setNumber(shoppingCart.getNumber());

                     //计算总金额
                     //计算总金额
                     BigDecimal bigDecimal1 = bigDecimal.add(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())));
                     return orderDetail;
                 }
         );*/

         //3.组装订单
        orders.setId(orderid);
        orders.setNumber(String.valueOf(orderid));
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setStatus(2);
        orders.setUserId(BaseContext.getCurrentId());
        orders.setUserName(user.getName());
        orders.setAmount(bigDecimal);
        orders.setAddress(addressBook.getDetail());

        //4.保存订单及订单明细
        this.save(orders);
        orderDetailService.saveBatch(orderDetails);

        //5.删除购物车
        shoppingCartService.remove(wrapper);

    }




}
